﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Collections.Generic;
using System.IO;
using System.Web.Services;
public partial class JOINT_frmJ_StaffCareerInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((JOINT_MstPage_JOINT)this.Master).changeTitle("職種職務情報登録");
        ((JOINT_MstPage_JOINT)this.Master).HideLiArea("frmJ_StaffCareerInput");
        if (Session["役"] == null || Session["役"].ToString() == "")
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
        if (!Page.IsPostBack)
        {


            bind資格コード();
            CheckUpdate();

            if (Session["IdEmp"] == null || Session["ModeUpdateStaffCareerInput"] == null)
            {

                Response.Redirect("frmJ_StaffBasicInfoInput.aspx");

            }
            if (Session["ModeUpdateStaffCareerInput"].Equals("Update"))
            {
                btn_登録.Text = "更新";
                int ID = Convert.ToInt32(Session["IdEmp"].ToString());
                職種職務情報Manager frm = new 職種職務情報Manager();
                DataTable dt = frm.select職種職務情報ByID(ID);
                if (dt.Rows.Count>0)
                {
                    ddl_職種.Text = dt.Rows[0]["職種コード"].ToString();
                    ddl_職種_SelectedIndexChanged(null, null);
                    ddl_職務.Text = dt.Rows[0]["職務コード"].ToString();
                    txt立場.Text = dt.Rows[0]["立場"].ToString();
                    txt具体的業務内容.Text = dt.Rows[0]["具体的業務内容"].ToString();
                    DateTime time = DateTime.Parse(dt.Rows[0]["実際開始日"].ToString());
                    txt実際開始日.Text = time.ToString("yyyy/MM/dd");
                    if (dt.Rows[0]["実際終了日"].ToString() != "")
                    {
                        DateTime date = DateTime.Parse(dt.Rows[0]["実際終了日"].ToString());
                        txt_実際終了日.Text = date.ToString("yyyy/MM/dd");
                    }
                    else
                    {
                        txt_実際終了日.Text = dt.Rows[0]["実際終了日"].ToString();
                    }
                    txt_変更理由.Text = dt.Rows[0]["変更理由"].ToString();

                }
            }
            if (Session["IdEmp"] != null)
            {

                if (Session["IdEmp"].ToString() != "")
                    login.Value = Session["IdEmp"].ToString();

            }
        }
        ddl_職種.Focus();
        RequiredFieldValidator1.ErrorMessage = GetError("ER00007");
        RequiredFieldValidator2.ErrorMessage = GetError("ER00007");
        RequiredFieldValidator3.ErrorMessage = GetError("ER00001");
        RequiredFieldValidator4.ErrorMessage = GetError("ER00001");
        RequiredFieldValidator5.ErrorMessage = GetError("ER00001");
        CustomValidator2.ErrorMessage = GetError("ER00005");
        CustomValidator7.ErrorMessage = GetError("ER00006");
        CustomValidator3.ErrorMessage = GetError("ER00006");
        CustomValidator職種コードAND職務コード.ErrorMessage = GetError("ER00009");
        // CustomValidator1.ErrorMessage =  GetError("ER00009");
    }
    protected void btn_戻る_Click(object sender, EventArgs e)
    {

        if (Session["ModeUpdateStaffCareerInput"].Equals("Update"))
        {
            Response.Redirect("frmJ_EmpBasicInfo.aspx");
        }
        else
        {
            Response.Redirect("frmJ_StaffDivisionInput.aspx");
        }
    }
    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmJ_StaffContactInput.aspx");
    }
    public String GetError(string errorid)
    {
        string error = "";
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        if (dt.Rows.Count > 0)
        {
            return error = dt.Rows[0][2].ToString();
        }
        else
        {
            return error;
        }
    }
    public void bind資格コード()// hien thi資格コード.
    {
        try
        {
            職種コードManager frm = new 職種コードManager();
            DataTable dt = frm.frmJ_StaffCareerInput_GetDLL職種コード();
            ddl_職種.DataSource = dt;
            ddl_職種.DataTextField = "職種名";
            ddl_職種.DataValueField = "ID";
            ddl_職種.DataBind();
            ListItem item = new ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_職種.Items.Insert(0, item);
        }
        catch { }
    }
    protected void ddl_職種_SelectedIndexChanged(object sender, EventArgs e)// dll o duoi load dua vao dll o tren
    {
        Page.Title = "職種職務情報登録";
        if (ddl_職種.SelectedValue.ToString() == "-1")
        {
            ddl_職務.SelectedValue = "-1";
            ddl_職務.Enabled = false;
        }
        if (ddl_職種.SelectedValue.ToString() != "-1")
        {
            ddl_職種.Focus();
            int 職種コードID = Convert.ToInt32(ddl_職種.SelectedValue.ToString());
            ddl_職務.Enabled = true;
            職務コードManager manager = new 職務コードManager();
            DataTable table = manager.frmJ_StaffCareerInput_GetDLL職務コード(職種コードID);
            ddl_職務.DataSource = table;
            ddl_職務.DataTextField = "職務名";
            ddl_職務.DataValueField = "ID";
            ddl_職務.DataBind();
            ListItem item = new ListItem("[選択]", "-1");
            item.Selected = true;
            ddl_職務.Items.Insert(0, item);
        }
    }

    public void CheckUpdate()// ham kiem tra update
    {
        if (Session["ModeUpdateStaffCareerInput"] != null)
        {
            if (Session["ModeUpdateStaffCareerInput"].Equals("Update"))
            {
                try
                {
                    if (Session["IDStaffCareerInput"] != null)
                    {
                        if (Session["IdEmp"].ToString() != "")
                            login.Value = Session["IdEmp"].ToString();
                        btn_次.Enabled = false;
                        ddl_職務.Enabled = true;
                        職種職務情報Manager frm = new 職種職務情報Manager();
                        DataTable dt = new DataTable();
                        職種職務情報 obj = new 職種職務情報();

                        obj.ID = Convert.ToInt32(Session["IdEmp"].ToString()); //ID duoc gan tu EmbasicInfo


                        dt = frm.Get職種職務情報ById(obj.ID);

                        foreach (DataRow row in dt.Rows)
                        {
                            ddl_職種.Items.Remove("-1");
                            ddl_職種.SelectedValue = row["職種コード"].ToString();
                            ddl_職務.Items.Remove("-1");
                            ddl_職務.SelectedValue = row["職務コード"].ToString();
                            txt立場.Text = row["立場"].ToString();
                            txt具体的業務内容.Text = row["具体的業務内容"].ToString();
                            string 実際開始日 = row["実際開始日"].ToString();
                            DateTime d1 = DateTime.Parse(実際開始日);
                            実際開始日 = d1.ToString("yyyy/MM/dd");
                            txt実際開始日.Text = 実際開始日;
                            string 実際終了日 = row["実際終了日"].ToString();
                            DateTime d3 = DateTime.Parse(実際終了日);
                            実際終了日 = d3.ToString("yyyy/MM/dd");
                            txt_実際終了日.Text = 実際終了日;
                            txt_変更理由.Text = row["変更理由"].ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
    protected void btn_登録_Click(object sender, EventArgs e)
    {
        職種職務情報Manager frm = new 職種職務情報Manager();
        // lay cac gia tri control gan vao obj
        職種職務情報 obj = new 職種職務情報();
        obj.廃止区分 = 0;
        if (ddl_職種.SelectedValue.ToString() != "-1")
        {
            obj.職種コード = Convert.ToInt32(ddl_職種.SelectedValue.ToString());
            obj.職種 = ddl_職種.SelectedItem.ToString();
        }
        if (ddl_職務.SelectedValue.ToString() != "-1")
        {
            obj.職務コード = Convert.ToInt32(ddl_職務.SelectedValue.ToString());
            obj.職務 = ddl_職務.SelectedItem.ToString();
        }
        obj.立場 = HttpUtility.HtmlDecode(txt立場.Text.Trim());
        obj.具体的業務内容 = HttpUtility.HtmlDecode(txt具体的業務内容.Text.Trim());
        if (txt実際開始日.Text.ToString() != "")
        {
            try
            {
                DateTime d1;
                d1 = Convert.ToDateTime(txt実際開始日.Text);
                obj.実際開始日 = d1;
            }
            catch
            {
            }
        }
        if (txt_実際終了日.Text.ToString() != "")
        {
            try
            {
                DateTime d1;
                d1 = Convert.ToDateTime(txt_実際終了日.Text);
                obj.実際終了日 = d1;
            }
            catch
            {
            }
        }
        obj.変更理由 = HttpUtility.HtmlDecode(txt_変更理由.Text.Trim());


        // check cac  mode insert hay update
        if (Session["ModeUpdateStaffCareerInput"] != null)
        {
            if (Session["ModeUpdateStaffCareerInput"].Equals("Update"))
            {
                    obj.ID = Convert.ToInt32(Session["IdEmp"].ToString());
                    frm.Update職種職務情報(obj);
                    Response.Redirect("frmJ_EmpBasicInfo.aspx");
            }
            else
            {
                if (Page.IsValid)
                {
                    obj.社員ID = Convert.ToInt32(Session["IdEmp"].ToString());
                    frm.Insert職種職務情報_Phuong(obj);
                    ddl_職種.SelectedIndex = 0;
                    ddl_職務.Enabled = false;
                    ddl_職務.SelectedIndex = -1;
                    Response.Redirect("frmJ_StaffContactInput.aspx");
                }
            }
        }
        Response.Redirect("frmJ_StaffCareerInput.aspx");

    }
    protected void CustomValidate職種コードAND職務コード(object source, ServerValidateEventArgs args)// validate  dll thu 2
    {
        職種職務情報Manager frm = new 職種職務情報Manager();
        職種職務情報 obj = new 職種職務情報();
        DataTable dt = new DataTable();
        int 職種コード = 0;
        int 職務コード = 0;
        if (Session["IdEmp"] != null)
        {
            int 社員ID = Convert.ToInt32(Session["IdEmp"].ToString());
            dt = frm.Get職種職務情報By社員ID(社員ID);
        }
        if (Session["IDStaffCareerInput"] != null)
        {
            int ID = Convert.ToInt32(Session["IDStaffCareerInput"].ToString());
            DataTable table = frm.Get職種職務情報ById(ID);
            if (table.Rows.Count > 0)
            {
                // gia tri giu nguyen ban dau
                int 社員ID = Convert.ToInt32(table.Rows[0][0].ToString());
                職種コード = Convert.ToInt32(table.Rows[0][1].ToString());
                職務コード = Convert.ToInt32(table.Rows[0][3].ToString());
                dt = frm.Get職種職務情報By社員ID(社員ID);
            }
        }
        if (dt.Rows.Count > 0)
        {
            args.IsValid = true;
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (args.Value.ToString().Equals(row["職務コード"].ToString()))
                    {
                        if (ddl_職種.SelectedValue.ToString().Equals(row["職種コード"].ToString()))
                        {
                            args.IsValid = false;
                            if (Session["ModeUpdateStaffCareerInput"].Equals("Update"))
                            {
                                int i = Convert.ToInt32(row["職種コード"].ToString());
                                int j = Convert.ToInt32(row["職務コード"].ToString());
                                if (i == 職種コード && j == 職務コード)
                                {
                                    args.IsValid = true;
                                }
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

    protected void ddl_職務_SelectedIndexChange(object sender, EventArgs e)
    {

        Page.Title = "職種職務情報登録";
        ddl_職務.Focus();

    }
    protected void btn_リセット_Click(object sender, EventArgs e)
    {
        ddl_職務.Enabled = false;
        Response.Redirect("frmJ_StaffCareerInput.aspx");
    }
    [WebMethod]
    public static bool IsExisted(string ID1, string ID2, string ManNhanVien)// phuong thuc  kiem tra du lieu co trung duoi db ko
    {
        var returnVal = false;
        try
        {
            職種職務情報Manager frm = new 職種職務情報Manager();
            int id1 = Convert.ToInt32(ID1);
            int id2 = Convert.ToInt32(ID2);
            int manhanvien = Convert.ToInt32(ManNhanVien);
            if (HttpContext.Current.Session["ModeUpdateStaffCareerInput"]=="Insert")
            {
                DataTable table = frm.checkDLL(id1, id2, manhanvien);
                if (table.Rows.Count > 0)
                    returnVal = true;
            }
            
        }
        catch (Exception Exception) { }

        return returnVal;
    }



}

